<?php 
class Paging
{
    function createPaging($total, $current, $perPage, $url = ''){
        $numPage = floor($total/ $perPage);
        if (($total/ $perPage) - $numPage > 0){
            $numPage += 1;
        }
        $html = '';
        if ($numPage == 1){
            return '';
        }
        if ($current == 1){
            $html .= 'Trang đầu | ';
            $html .= 'Lùi | ';
        } else {
            $html .= "<a href='$url&p=1'>Trang đầu</a> | ";
            $html .= "<a href='$url&p=".($current - 1). "'>Lùi</a> | ";
        }
        if($current <=3){
            for($i=1; ($i<=5) and ($i <= $numPage); $i++){
                if ($i == $current){
                    $html .= $i. ' | ';
                }else{
                    $html .= "<a href='$url&p=$i'>[$i]</a> | ";
                }
            }
        }else{
            if ($numPage >= $current + 2){
                for($i=$current-2; ($i <=$current+2) and ($i <= $numPage); $i++){
                    if ($i == $current){
                        $html .= $i. ' | ';
                    }else{
                        $html .= "<a href='$url&p=$i'>[$i]</a> | ";
                    }
                }
            }else{
                for($i = $numPage - 4;$i <= $numPage; $i++){
                    if($i > 0){
                        if ($i == $current){
                            $html .= $i. ' | ';
                        }else{
                            $html .= "<a href='$url&p=$i'>[$i]</a> | ";
                        }
                    }                        
                }
            }
        }
        
        if ($current == $numPage){
            $html .= 'Tới | ';
            $html .= 'Cuối';
        } else {
            $html .= "<a href='$url&p=".($current + 1). "'>Tới</a> | ";
            $html .= "<a href='$url&p=$numPage'>Cuối</a>";
        }
        return $html;
    }
} 

?>